//
// Created by Huiyi on 2021/5/26.
//
#include "module_XExpand.h"


//窗口_重画
bool X_Form_Repaint(HWND hWnd,bool EraseBg){
    return InvalidateRect(hWnd,NULL,EraseBg);
}

//窗口_隐藏任务按钮
void X_Form_HideTaskButton(HWND hWnd){
    SetWindowLongPtr(hWnd,-8,(long)GetDesktopWindow());
}

//窗口_是否响应
bool X_Form_IfNormal(HWND hWnd){
    return SendMessageTimeout(hWnd,0,0,0,SMTO_ABORTIFHUNG,128,0)!=0;
}

//窗口_取光标坐标
bool X_Form_GetCaretPos (POINT& Pos) {
    return GetCaretPos(&Pos);
}

//窗口_取桌面句柄A
HWND X_Form_GetDesktopHWndA() {
    return FindWindowA("Progman", "Program Manager");
}

//窗口_取桌面句柄W
HWND X_Form_GetDesktopHWndW() {
    return FindWindowW(L"Progman", L"Program Manager");
}

//窗口_置父
HWND X_Form_SetParent(HWND hWndChild,HWND hWndParent){
    return SetParent(hWndChild,hWndParent);
}

//窗口_置位置和大小
bool X_Form_SetPosAndSize(HWND hWnd,int Left,int Top,int NewWidth,int NewHigh){
    RECT lpRect;
    GetWindowRect (hWnd, &lpRect);
    if (!Left){
        Left = lpRect.left;
    }
    if (!Top){
        Top = lpRect.top;
    }
    if (!NewWidth){
        NewWidth = lpRect.right - lpRect.left;
    }
    if (!NewHigh){
        NewHigh = lpRect.bottom - lpRect.top;
    }
    return MoveWindow(hWnd,Left,Top,NewWidth,NewHigh,true);
}

bool EnumWindowsProc (HWND hwnd,void*  lParam){
    HWND defview = FindWindowExA (hwnd, NULL, "SHELLDLL_DefView", NULL);
    if(!defview){
        (*(HWND*)lParam) = FindWindowExA (NULL, hwnd, "WorkerW", NULL);
    }
    return true;
}

HWND GetWorkerW(){
    DWORD result=0;
    HWND lParam=0;
    HWND hDistop = FindWindowA("Progman", "\0");
    SendMessageTimeoutA (hDistop, 1324, 0, 0, SMTO_NORMAL, 1000, &result);
    EnumWindows ((WNDENUMPROC)EnumWindowsProc, (int)&lParam);
    ShowWindow (lParam, SW_HIDE);
    return hDistop;
}

//窗口_嵌入桌面
HWND X_Form_EmbedOnDesktop (HWND hWnd){
    HWND newwnd = SetParent(hWnd,GetWorkerW());
    X_Form_SetPosAndSize(hWnd);
    return newwnd;
}
